package sysmgr.dao.impl;
import commmon.db.DbConnection;
import commmon.db.JdbcConnectionPool;
import sysmgr.bean.BookCommodity;
import sysmgr.bean.Page;
import sysmgr.bean.StaffAddress;
import sysmgr.dao.StaffDateDao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 类的描述：
 *
 * @author cwn
 * @since 2020/12/24 9:25
 */
public class StaffDateDaoImpl implements StaffDateDao {

    /**
     * 保存数据
     *
     * @param staffDate 用户数据对象
     * @return 是否保存成功
     */
    @Override
    public boolean saveStaffDate(BookCommodity staffDate) {
        return false;
    }

    /**
     * 删除数据
     *
     * @param staffDate 用户数据对象
     * @return 是否保存成功
     */
    @Override
    public boolean removeStaffDate(BookCommodity staffDate) {
        return false;
    }

    /**
     * 查询所有的用户数据对象，携带分页
     *
     * @param page 分页对象
     * @return 封装了用户数据对象的集合
     */
    @Override
    public List<BookCommodity> queryStaffDateAll(Page page, BookCommodity bookCommodity) {
        try {
            //创建连接对象
            Connection conn = JdbcConnectionPool.getConnection();
            String sql = "select * from book_commodity";
            //进行条件的拼接,判断如果不为null且不为空字符串
            if (null != bookCommodity.getBookname() && !"".equals(bookCommodity.getBookname())) {
                //如果传入了条件，就将条件拼接到后面，否则就不拼接
                sql += " where bookname like '%" + bookCommodity.getBookname() + "%'";
            }
            ResultSet q = (ResultSet) DbConnection.toExecuteMethod(conn, sql, null, "Q");
            Object staff1 = DbConnection.getObjectFromResultSetList(q, BookCommodity.class);
            List<BookCommodity> staffDateList = (List<BookCommodity>) staff1;
            if (staff1 != null) {
                return staffDateList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteStaffDateAll(BookCommodity bookCommodity) {
        try {
            //创建连接对象
            Connection conn = JdbcConnectionPool.getConnection();
            String sql = "delete from book_commodity where id = "+bookCommodity.getId();
            Integer d = (Integer) DbConnection.toExecuteMethod(conn, sql, null, "D");
            if (d == 1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
/*    @Override
    public List<BookCommodity> insertStaffDateAll(Page page, BookCommodity bookCommodity) {
        try {
            //创建连接对象
            Connection conn = JdbcConnectionPool.getConnection();
            String sql = "insert * from book_commodity";
            //进行条件的拼接,判断如果不为null且不为空字符串
            if (null != bookCommodity.getBookname() && !"".equals(bookCommodity.getBookname())) {
                //如果传入了条件，就将条件拼接到后面，否则就不拼接
                sql += " where bookname like '%" + bookCommodity.getBookname() + "%'";
            }
            ResultSet i = (ResultSet) DbConnection.toExecuteMethod(conn, sql, null, "I");
            Object staff1 = DbConnection.getObjectFromResultSetList(i, BookCommodity.class);
            List<BookCommodity> staffDateList = (List<BookCommodity>) staff1;
            if (staff1 != null) {
                return staffDateList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }*/
@Override
public boolean updateStaffDateAll(BookCommodity bookCommodity) {
    try {
        //创建连接对象
        Connection conn = JdbcConnectionPool.getConnection();
        String sql = "update book_commodity set bookname ='"+bookCommodity.getBookname()+"' where id ="+bookCommodity.getId();
        Integer u = (Integer) DbConnection.toExecuteMethod(conn, sql, null, "U");
        if (u == 1){
            return true;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}
    }



